package com.xxx.reportingsystem.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.pojo.LoginUser;
import com.xxx.pojo.Menu;
import com.xxx.reportingsystem.service.MenuService;
import com.xxx.service.UserOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @Autowired
    private UserOperateService userOperateService;

    @Transactional
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('sys:add:menu')")
    public Result addMenu(@RequestBody Menu menu){
        if (Objects.isNull(menu)){
            throw new RuntimeException("当前权限格式错误!");
        }
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        menu.setCreateUser(loginUser.getUser().getName());
        menu.setUpdateUser(loginUser.getUser().getName());
        LocalDateTime now = LocalDateTime.now();
        menu.setCreateTime(now);
        menu.setUpdateTime(now);
        boolean save = menuService.save(menu);
        if (!save){
            throw new RuntimeException("添加失败");
        }
        userOperateService.addLogManage("权限管理","添加权限","添加");
        return Result.success("添加权限成功");
    }

    @Transactional
    @PostMapping("/select")
    public Result selectByCondition(@RequestBody Menu menu, @RequestParam("current") Integer current, @RequestParam("size") Integer size){
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper();
        wrapper.like(menu.getMenuName() != null && menu.getMenuName().length() > 0,Menu::getMenuName,menu.getMenuName())
                .like(menu.getMenuId() != null && menu.getMenuId() > 0,Menu::getMenuId,menu.getMenuId())
                .like(menu.getStatus() != null,Menu::getStatus,menu.getStatus())
                .eq(Menu::getDelete,0);
        IPage iPage = new Page(current,size);
        IPage page = menuService.page(iPage,wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",page.getRecords());
        map.put("total", page.getTotal());
        return Result.success("",map);
    }

    @Transactional
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('sys:update:menu')")
    public Result updateMenu(@RequestBody Menu menu){
        if (Objects.isNull(menu)){
            throw new RuntimeException("Menu数据传输错误");
        }
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        menu.setUpdateUser(loginUser.getUser().getName());
        menu.setUpdateTime(LocalDateTime.now());
        boolean b = menuService.updateById(menu);
        if (!b){
            throw new RuntimeException("修改角色失败！");
        }
        userOperateService.addLogManage("权限管理","修改权限","修改");
        return Result.success("修改角色成功!");
    }

    @Transactional
    @DeleteMapping("/delete")
    @PreAuthorize("hasAnyAuthority('sys:delete:menu')")
    public Result deleteMenu(@RequestParam("menuIds") List<Integer> menuIds){
        boolean b = false;
        if (!Objects.isNull(menuIds)){
            for (Integer menuId : menuIds) {
                b = menuService.removeById(menuId);
            }
        }
        if (!b){
            throw new RuntimeException("删除角色失败！");
        }
        userOperateService.addLogManage("权限管理","删除权限","删除");
        return Result.success("删除权限成功!");
    }
}
